<?php

class Timer
{
    private static $timers = array();
    private static $log = null;

    private function __construct() {}

    public static function __construct_static()
    {
        $dir = ROOT_PATH."logs/";
        if ( !file_exists($dir) )
        {
            mkdir($dir);
        }
        $date = strftime("%Y%m%d");
        self::$log = fopen( $dir."timers_$date.log", "a+t" );
    }

    public static function Start( $id, $message = '' )
    {
        if ( !isset(self::$timers[$id]) )
        {
            self::$timers[$id] = array();
        }

        self::$timers[$id][] = array(
            'time'      => microtime(true),
            'message'   => $message
        );
    }

    public static function Stop( $id )
    {
        if ( isset(self::$timers[$id]) )
        {
            $timer = array_pop(self::$timers[$id]);
            if ( count(self::$timers[$id]) == 0 )
            {
                unset(self::$timers[$id]);
            }

            fwrite(self::$log, sprintf("%s : $id [%.5f] %s\n", strftime("%H:%M:%S", time()), microtime(true) - $timer['time'], $timer['message']));
            fflush(self::$log);
        }
    }
}

?>
